package com.platform.data.handle.converter.impl;

import com.platform.data.common.CommonConstant;
import com.platform.data.handle.converter.AbstractDataTypeProcessHandler;
import com.platform.data.handle.converter.DataTypeEnum;
import org.apache.commons.lang3.math.NumberUtils;

/**
 * 日期格式
 *
 * @author zhuweijun
 * @version 1.0.0
 * @since 2025/5/13 17:04
 */
public class DateTypeProcessHandler extends AbstractDataTypeProcessHandler {

    /**
     * 日期格式
     *
     * @param typeEnum typeEnum
     * @param fieldName fieldName
     * @param fieldValue fieldValue
     * @return {@link Object}
     */
    @Override
    public Object process(DataTypeEnum typeEnum, String fieldName, String fieldValue) {
        // 检查是否是横杠：-
        if(fieldValue.contains(CommonConstant.DEFAULT_HORIZONTAL)){
            return fieldValue;
        }

        String[] splitDate = fieldValue.split(CommonConstant.DEFAULT_SLASH);
        if(splitDate.length == CommonConstant.THREE){
            String year = splitDate[CommonConstant.ZERO];
            String month = splitDate[CommonConstant.ONE];
            String day = splitDate[CommonConstant.TWO];

            // 校验是否数字
            if(!NumberUtils.isCreatable(year)
                    || !NumberUtils.isCreatable(month)
                    || !NumberUtils.isCreatable(day)){
                log.warn("DateTypeProcessHandler.process|属性字段赋值，输入日期非数字，无法转换格式，反馈原值，属性名称：{}，属性值：{}", fieldName, fieldValue);
                return fieldValue;
            }
            // 对月补0
            int monthInt = Integer.parseInt(month);
            if(monthInt <= CommonConstant.NINE){
                month = CommonConstant.ZERO + month;
            }

            // 对日补0
            int dayInt = Integer.parseInt(day);
            if(dayInt <= CommonConstant.NINE){
                day = CommonConstant.ZERO + day;
            }
            return year + CommonConstant.DEFAULT_HORIZONTAL + month + CommonConstant.DEFAULT_HORIZONTAL + day;
        }
        return fieldValue;
    }
}
